package com.mvvm.start.common.utils

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.mvvm.start.base.BaseApplication
import com.permissionx.guolindev.PermissionX

object PermissionXUtils {
    fun requestPermissions(
        context: Fragment,
        permissions: MutableList<String>,
        agreeblock: () -> Unit,
        refuseblock: () -> Unit,
        description: String? = "MVVMStart可能需要一些权限才能使用该功能",
    ) {
        PermissionX.init(context)
            .permissions(
                permissions
            ).onExplainRequestReason { scope, deniedList ->
                scope.showRequestReasonDialog(deniedList, description, "允许", "拒绝")
            }
            .onForwardToSettings { scope, deniedList ->
                scope.showForwardToSettingsDialog(deniedList, "您需要去应用程序设置当中手动开启权限", "我已明白", "取消")
            }
            .request { allGranted, grantedList, deniedList ->
                if (allGranted) {
                    agreeblock()
                } else {
                    refuseblock()
                }
            }
    }

    fun requestPermissions(
        context: FragmentActivity,
        permissions: MutableList<String>,
        agreeblock: () -> Unit,
        refuseblock: () -> Unit,
        description: String? = "MVVMStart可能需要一些权限才能使用该功能",
    ) {
        PermissionX.init(context)
            .permissions(
                permissions
            )
            .onExplainRequestReason { scope, deniedList ->
                scope.showRequestReasonDialog(deniedList, description, "允许", "拒绝")
            }
            .onForwardToSettings { scope, deniedList ->
                scope.showForwardToSettingsDialog(deniedList, "您需要去应用程序设置当中手动开启权限", "我已明白", "取消")
            }
            .request { allGranted, grantedList, deniedList ->
                if (allGranted) {
                    agreeblock()
                } else {
                    refuseblock()
                }
            }
    }

    fun isAgreePermisson(permissions: String) =
        PermissionX.isGranted(BaseApplication.context, permissions)

}